﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Media.Imaging;

namespace AzureBlobManager
{
    [XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/SPAzureBlobField.Services")]
    public class ArrayOfBlob : List<Blob>
    {
    }

    public class Blob : INotifyPropertyChanged
    {
        private string _name;
        private double _bytesUploaded;
        private bool _inProgress = false;
        private int _percentage;
        private ImageSource _imageSource;

        public Blob()
        {
            StatusImage = new BitmapImage(new Uri("Images/pause.png", UriKind.Relative));
        }

        [XmlElement]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        [XmlElement]
        public string Guid;

        [XmlElement]
        public string SharedAccessSignature;

        [XmlElement]
        public bool IsUpload;

        [XmlIgnore]
        public bool Deleted = false;

        [XmlIgnore]
        public string Extenstion;

        [XmlIgnore]
        public FileState State { get; set; }

        [XmlIgnore]
        public double FileSize
        {
            get;
            set;
        }

        [XmlIgnore]
        public bool InProgress
        {
            get { return _inProgress; }
            set
            {
                _inProgress = value;
                NotifyPropertyChanged("InProgress");
            }
        }

        [XmlIgnore]
        public double BytesUploaded
        {
            get { return _bytesUploaded; }
            set
            {
                _bytesUploaded = value;
                NotifyPropertyChanged("BytesUploaded");
                Percentage = (int)((value * 100) / FileSize);
            }
        }

        [XmlIgnore]
        public int Percentage
        {
            get { return _percentage; }
            set
            {
                _percentage = value;
                NotifyPropertyChanged("Percentage");
            }
        }

        [XmlIgnore]
        public ImageSource StatusImage
        {
            get { return _imageSource; }
            set
            {
                _imageSource = value;
                NotifyPropertyChanged("StatusImage");
            }
        }

        [XmlIgnore]
        public IAzureTranfertManager Manager;

        private void NotifyPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }
    
        public event PropertyChangedEventHandler  PropertyChanged;
}

    public enum FileState : int
    {
        Pending = 0,
        Uploading = 1,
        Finished = 2,
        Deleted = 3,
        Error = 4,
        Cancel = 5
    }
}
